//-----------------Sonic CD Fan Script-------------------//
// Yeah...
// Its port from Sonic 1(2013)

// Aliases
#alias Object.Value0	:	Object.Timer
#alias Object.Value1	:	Object.YVelocity
#alias Object.Value2	:	Object.T4
#alias Object.Value3	:	Object.TimerExtra
#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod


#alias Object.PropertyValue		:	PushButton.Pressed	

 
// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

// States
#alias 0	:	FAN_ON
#alias 1	:	FAN_OFF

sub ObjectMain
	if PushButton[-2].Pressed == false
		Object.T4 = 0
	end if

	if Object.T4 == 0
		if PushButton[-2].Pressed == true
			if	Object.State == FAN_ON
				Object.State    = FAN_OFF
			end if
			if	Object.State == FAN_OFF
				Object.State    = FAN_ON
			end if
			Object.T4 = 1
		end if
	end if
end sub

sub ObjectPlayerInteraction
	switch Object.State
		case FAN_ON
			Object.TimerExtra++
			if Object.TimerExtra == 300
				Object.State = FAN_OFF
				Object.TimerExtra = 0
			end if
			PlayerObjectCollision(C_TOUCH, -8, -8, 8, 16) //Detect
			if CheckResult == true
				CallFunction(Player_Hit)
			end if
			PlayerObjectCollision(C_TOUCH, -128, -128, 128, 0) //Detect
			if CheckResult == true
				TempValue0 = Player.xpos
				TempValue0 &= -0x10000
				TempValue0 -= object.xpos
				TempValue0 += 0x500000
				if TempValue0 >= 0
					if TempValue0 < 0xF00000
						TempValue1 = Player.ypos
						TempValue1 &= -0x10000
						TempValue1 -= object.ypos
						TempValue1 += 0x600000
						GetBit(TempValue2, object.propertyValue, 2)
						if TempValue2 == 1
							TempValue3 = 0x900000
						else
							TempValue3 = 0x700000
						end if
						if TempValue1 >= 0
							if TempValue1 < TempValue3
								TempValue0 -= 0x500000
								if TempValue0 < 0
									TempValue0 ^= -0x10000
									TempValue0 <<= 1
								end if
								TempValue0 += 0x600000
								TempValue0 ^= 0xFF0000
								TempValue0 += 0x10000
								TempValue0 >>= 4
								GetBit(TempValue1, object.propertyValue, 0)
								if TempValue1 != 0
									FlipSign(TempValue0)
								end if
								if Object.PropertyValue == 0
									Player.xpos -= TempValue0
								else
									Player.xpos += TempValue0
									Object.Direction = FACING_RIGHT
								end if	
							end if
						end if
					end if
				end if
			end if
			break
		case FAN_OFF
			Object.TimerExtra++
			if Object.TimerExtra == 300
				Object.State = FAN_ON
				Object.TimerExtra = 0
			end if
			break
		end switch
end sub

sub ObjectDraw
	if Object.State == FAN_ON
		Object.Timer--
		if Object.Timer < 0
			Object.Timer = 1
			if Object.Frame > 1
				Object.Frame = 0
			else
				Object.Frame++
			end if
		end if
	end if
		DrawSpriteFX(Object.Frame, FX_FLIP, Object.XPos, Object.Ypos)
end sub


sub ObjectStartup
	LoadSpriteSheet("R4.5/Objects2.gif")

	TempValue0 = HUD.CurrentTimePeriod
	TempValue1 = 1
	TempValue0 *= 33
	TempValue1 += TempValue0 
	SpriteFrame(-15, -16, 32, 32, TempValue1, 156)	// #0 - Fan 1 
	SpriteFrame(-16, -16, 32, 32, TempValue1, 189)	// #1 - Fan 2
	SpriteFrame(-16, -16, 32, 32, TempValue1, 222)	// #2 - Fan 3
end sub

// ========================
// Editor Subs
// ========================


sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("R4.5/Objects2.gif")
	SpriteFrame(-15, -16, 32, 32, 1, 156)	// #0 - Fan 1

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
